﻿Imports System.Data.SqlClient

Public Class clsAccidentes
    Private mintidAccidente As Integer
    Private mintidEmpleado As Integer
    Private mdatFecha As Date
    Private mstrHora As String
    Private mstrLugar As String
    Private mstrResponsabilidad As String
    Private mstrSeveridad As String
    Private mdblMonto As Double
    Private mdblCobro As Double
    Private mstrRuta As String
    Private mstrAutobus As String
    Private mstrCausa As String
    Private mstrUsuario As String
    Private mstrTipo As String
    Private mstrReportado As String
    Private mstrEnviado As String
    Private mstrFolio As String
    Private mstrDestino As String
    Private mdatFechaRegistro As Date

    Public Property idAccidente() As Integer
        Get
            Return mintidAccidente
        End Get
        Set(ByVal value As Integer)
            mintidAccidente = value
        End Set
    End Property
    Public Property idempleado() As Integer
        Get
            Return mintidEmpleado
        End Get
        Set(ByVal value As Integer)
            mintidEmpleado = value
        End Set
    End Property
    Public Property fecha() As Date
        Get
            Return mdatFecha
        End Get
        Set(ByVal value As Date)
            mdatFecha = value
        End Set
    End Property
    Public Property Hora() As Date
        Get
            Return mstrHora
        End Get
        Set(ByVal value As Date)
            mstrHora = value
        End Set
    End Property
    Public Property Lugar() As String
        Get
            Return mstrLugar
        End Get
        Set(ByVal value As String)
            mstrLugar = value
        End Set
    End Property
    Public Property Responsabilidad() As String
        Get
            Return mstrResponsabilidad
        End Get
        Set(ByVal value As String)
            mstrResponsabilidad = value
        End Set
    End Property
    Public Property Severidad() As String
        Get
            Return mstrSeveridad
        End Get
        Set(ByVal value As String)
            mstrSeveridad = value
        End Set
    End Property
    Public Property Monto() As Double
        Get
            Return mdblMonto
        End Get
        Set(ByVal value As Double)
            mdblMonto = value
        End Set
    End Property
    Public Property Cobro() As Double
        Get
            Return mdblCobro
        End Get
        Set(ByVal value As Double)
            mdblCobro = value
        End Set
    End Property
    Public Property Ruta() As String
        Get
            Return mstrRuta
        End Get
        Set(ByVal value As String)
            mstrRuta = value
        End Set
    End Property
    Public Property Autobus() As String
        Get
            Return mstrAutobus
        End Get
        Set(ByVal value As String)
            mstrautobus = value
        End Set
    End Property
    Public Property Causa() As String
        Get
            Return mstrCausa
        End Get
        Set(ByVal value As String)
            mstrCausa = value
        End Set
    End Property
    Public Property Usuario() As String
        Get
            Return mstrUsuario
        End Get
        Set(ByVal value As String)
            mstrUsuario = value
        End Set
    End Property
    Public Property Tipo() As String
        Get
            Return mstrTipo
        End Get
        Set(ByVal value As String)
            mstrTipo = value
        End Set
    End Property
    Public Property Reportado() As String
        Get
            Return mstrReportado
        End Get
        Set(ByVal value As String)
            mstrReportado = value
        End Set
    End Property
    Public Property Enviado() As String
        Get
            Return mstrEnviado
        End Get
        Set(ByVal value As String)
            mstrEnviado = value
        End Set
    End Property
    Public Property Folio() As String
        Get
            Return mstrFolio
        End Get
        Set(ByVal value As String)
            mstrFolio = value
        End Set
    End Property
    Public Property Destino() As String
        Get
            Return mstrDestino
        End Get
        Set(ByVal value As String)
            mstrDestino = value

        End Set
    End Property
    Public Property FechaRegistro() As Date
        Get
            Return mdatFechaRegistro
        End Get
        Set(ByVal value As Date)
            mdatFechaRegistro = value
        End Set
    End Property

    Public Sub New()
        mintidAccidente = 0
        mintidEmpleado = 0
        mdatFecha = Now()
        mstrHora = ""
        mstrLugar = ""
        mstrResponsabilidad = ""
        mstrSeveridad = ""
        mdblMonto = 0
        mdblCobro = 0
        mstrRuta = ""
        mstrAutobus = ""
        mstrCausa = ""
        mstrUsuario = ""
        mstrTipo = ""
        mstrReportado = ""
        mstrEnviado = ""
        mstrFolio = ""
        mstrDestino = ""
        mdatFechaRegistro = Now()



    End Sub
    
    Public Sub Borrar()
        Call SetSqlCitra("Delete From Accidentes Where idAccidente= " & idAccidente)

    End Sub
    Public Sub Buscar()
        Dim cnConn As New sqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnConn
        cmdBuscar.CommandText = "SELECT * From Accidentes " & _
        " WHERE idAccidente=" & idAccidente

        Dim rdBuscar As SqlDataReader

        cnConn.Open()
        rdBuscar = cmdBuscar.ExecuteReader

        If rdBuscar.Read() Then
            mintidAccidente = rdBuscar("idaccidente")
            mintidEmpleado = rdBuscar("idempleado")
            mdatFecha = rdBuscar("fecha")
            mstrHora = rdBuscar("hora")
            mstrLugar = rdBuscar("lugar").ToString.Trim
            mstrResponsabilidad = rdBuscar("responsabilidad").ToString.Trim
            mstrSeveridad = rdBuscar("severidad").ToString.Trim
            mdblMonto = rdBuscar("monto")
            mdblCobro = rdBuscar("cobro")
            mstrRuta = rdBuscar("ruta").ToString.Trim
            mstrAutobus = rdBuscar("autobus").ToString.Trim
            mstrCausa = rdBuscar("causa").ToString.Trim
            mstrUsuario = rdBuscar("usuario").ToString.Trim
            mstrTipo = rdBuscar("tipo").ToString.Trim
            mstrReportado = rdBuscar("reportado").ToString.Trim
            mstrEnviado = rdBuscar("enviado").ToString.Trim
            mstrFolio = rdBuscar("folio").ToString.Trim
            mstrDestino = rdBuscar("destino").ToString.Trim
            mdatFechaRegistro = rdBuscar("fecharegistro")
        Else
            mintidAccidente = 0
            mintidEmpleado = 0
            mdatFecha = Now()
            mstrHora = Now()
            mstrLugar = ""
            mstrResponsabilidad = ""
            mstrSeveridad = ""
            mdblMonto = 0
            mdblCobro = 0
            mstrRuta = ""
            mstrAutobus = ""
            mstrCausa = ""
            mstrUsuario = ""
            mstrTipo = ""
            mstrReportado = ""
            mstrEnviado = ""
            mstrFolio = ""
            mstrDestino = ""
            mdatFechaRegistro = Now()
        End If

        rdBuscar.Close()
        cnConn.Close()


    End Sub
    Public Sub Insertar()
        Dim sql As String = "Insert Into Accidentes (" & _
                            "IdAccidente," & _
                            "IdEmpleado," & _
                            "Fecha," & _
                            "Hora," & _
                            "Lugar," & _
                            "Responsabilidad," & _
                            "Severidad," & _
                            "Monto," & _
                            "Cobro," & _
                            "Ruta," & _
                            "Autobus," & _
                            "Causa," & _
                            "Usuario," & _
                            "Tipo," & _
                            "Reportado," & _
                            "Enviado," & _
                            "Folio," & _
                            "Destino," & _
                            "FechaRegistro)" & _
                            "VALUES (" & _
                            "@IdAccidente," & _
                            "@IdEmpleado," & _
                            "@Fecha," & _
                            "substring(@Hora,1,10)," & _
                            "substring(@Lugar,1,80)," & _
                            "substring(@Responsabilidad,1,10)," & _
                            "substring(@Severidad,1,10)," & _
                            "@Monto," & _
                            "@Cobro," & _
                            "substring(@Ruta,1,100)," & _
                            "substring(@Autobus,1,20)," & _
                            "substring(@Causa,1,200)," & _
                            "substring(@Usuario,1,20)," & _
                            "substring(@Tipo,1,15)," & _
                            "substring(@Reportado,1,10)," & _
                            "substring(@Enviado,1,10)," & _
                            "substring(@Folio,1,10)," & _
                            "substring(@Destino,1,20)," & _
                            "@FechaRegistro)"


        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim Command As New SqlCommand(sql, cnConn)

        Command.Parameters.AddWithValue("IdAccidente", mintidAccidente)
        Command.Parameters.AddWithValue("IdEmpleado", mintidEmpleado)
        Command.Parameters.AddWithValue("Fecha", mdatFecha)
        Command.Parameters.AddWithValue("Hora", mstrHora)
        Command.Parameters.AddWithValue("Lugar", mstrLugar)
        Command.Parameters.AddWithValue("Responsabilidad", mstrResponsabilidad)
        Command.Parameters.AddWithValue("Severidad", mstrSeveridad)
        Command.Parameters.AddWithValue("Monto", mdblMonto)
        Command.Parameters.AddWithValue("Cobro", mdblCobro)
        Command.Parameters.AddWithValue("Ruta", mstrRuta)
        Command.Parameters.AddWithValue("Autobus", mstrAutobus)
        Command.Parameters.AddWithValue("Causa", mstrCausa)
        Command.Parameters.AddWithValue("Usuario", mstrUsuario)
        Command.Parameters.AddWithValue("Tipo", mstrTipo)
        Command.Parameters.AddWithValue("Reportado", mstrReportado)
        Command.Parameters.AddWithValue("Enviado", mstrEnviado)
        Command.Parameters.AddWithValue("Folio", mstrFolio)
        Command.Parameters.AddWithValue("Destino", mstrDestino)
        Command.Parameters.AddWithValue("FechaRegistro", mdatFechaRegistro)

        cnConn.Open()
        Dim RowsAffected As Integer = Command.ExecuteNonQuery

        If RowsAffected > 0 Then
            cnConn.Close()
        Else
            MsgBox("No fue posible insertar el Registro ", MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If
    End Sub
End Class
